<?php
/**
  * Mar 5, 2008
  *
  * Coded by: Ropu
  *
  */


class AppVersionDAO {
  
  public static function get(AppVersion &$app_version) {
    $db = DB::getDB();
    $sql = "SELECT url, uploaded, version, public, opensource, date 
            FROM app_version 
            WHERE id = ?";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $app_version->getId());
      $stmt->bind_result($url, $uploaded, $version, $public, 
                         $opensource, $date);
      $stmt->execute();
      if(!$stmt->fetch()) {
        $stmt->close();
        throw new Exception('Invalid appVersion with id: ' . 
                              $app_version->getId());
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
      $app_version->setUrl($url);
      $app_version->setUploaded($uploaded);
      $app_version->setVersion($version);
      $app_version->setPublic($public);
      $app_version->setOpensource($opensource);
      $app_version->setDate($date);
    }
    return $app_version->getId();
  }
  
  public static function getVersions(App &$app) {
    $db = DB::getDB();
    $appVersions = array();
    $appVersions_ids = array();
    $sql = "SELECT id FROM app_version 
            WHERE app_id = ? order by date asc";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $app->getId());
      $stmt->bind_result($id);
      $stmt->execute();
      
      while($stmt->fetch()) {
        $appVersions_ids[] = $id;
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
      foreach($appVersions_ids as $id) {
        $appVersions[$id] = new AppVersion($id);
      }
    }
    return $appVersions;
  }
  
  public static function insert(AppVersion &$app_version, App $app) {
    $db = DB::getDB();    
    if ($stmt = $db->prepare("INSERT INTO  `app_version` (
                                            `url` ,
                                            `uploaded` ,
                                            `version` ,
                                            `app_id` ,
                                            `public` ,
                                            `opensource` ,
                                            `date`
                                            )
                                            VALUES (
                                            ?,?,?,?,?,?,now()
                                            ) ")) {
      $stmt->bind_param("sisiii", $app_version->getUrl(), 
                                 $app_version->isUploaded(),
                                 $app_version->getVersion(),
                                 $app->getId(),
                                 $app_version->isPublic(), 
                                 $app_version->isOpensource()
                        );
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $stmt->close();
    }
    return $db->insert_id;
  }
  
  
  
}